package com.djhu.newscollector.crawel.net;

import com.djhu.newscollector.crawel.core.Url;
import org.apache.commons.io.IOUtils;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * Created by zw on 2018/2/11.
 */
public class DefaultHttpVisitor implements HttpVisitor{
    static {
        TrustManager[] trustAllCerts = new TrustManager[]{
                new X509TrustManager() {
                    public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                        return null;
                    }

                    public void checkClientTrusted(
                            java.security.cert.X509Certificate[] certs, String authType) {
                    }

                    public void checkServerTrusted(
                            java.security.cert.X509Certificate[] certs, String authType) {
                    }
                }
        };

        try {
            SSLContext sc = SSLContext.getInstance("SSL");
            sc.init(null, trustAllCerts, new java.security.SecureRandom());
            HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
        } catch (Exception ex) {
           // LOG.info("Exception", ex);
        }
    }

    @Override
    public Page visit(HttpRequest request) throws IOException {

        URL url = new URL(request.getUrl().getUrl());
        int code = -1;
        HttpURLConnection con = null;
        InputStream is = null;
        con = (HttpURLConnection) url.openConnection();
        con.setDoInput(true);
        con.setDoOutput(true);
        con.setConnectTimeout(10*1000);
        con.setReadTimeout(30*1000);
        con.setRequestMethod("GET");
        con.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36");
        code = con.getResponseCode();
        if (code != 200){
            return null;
        }


        is = con.getInputStream();
        byte[] content = null;
        try {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            byte[] buf = new byte[2048];
            int read;
            int sum = 0;
            int maxsize = Integer.MAX_VALUE;
            while ((read = is.read(buf)) != -1) {
                if (maxsize > 0) {
                    sum = sum + read;

                    if (maxsize > 0 && sum > maxsize) {
                        read = maxsize - (sum - read);
                        bos.write(buf, 0, read);
                        break;
                    }
                }
                bos.write(buf, 0, read);
            }
            content = bos.toByteArray();
        }catch (Exception e){

        }finally {
            IOUtils.closeQuietly(is);
        }

        HttpResponse response = new HttpResponse();
        response.setCode(code);
        response.setRequest(request);
        response.setBytes(content);
        Page page = new Page();
        page.setResponse(response);
        return page;
    }

    public static void main(String[] args) throws IOException {
        Url url = new Url();
        url.setUrl("https://www.s6tu.com/images/2018/01/04/1220_120.jpg");
        HttpRequest request =new HttpRequest();
        request.setUrl(url);

        DefaultHttpVisitor visitor = new DefaultHttpVisitor();
        Page page = visitor.visit(request);


    }
}